package com.example.money.test;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class NotificationActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mNoti1;
    private Button mNoti2;
    private Button mNoti3;
    private NotificationManager mNotificationManager;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_layout);

        initChannel();
        init();

    }

    private void initChannel() {

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            createChinnel("generate","普通通知",NotificationManager.IMPORTANCE_LOW);
            createChinnel("fold","折叠通知",NotificationManager.IMPORTANCE_HIGH);
            createChinnel("suspension","悬浮通知",NotificationManager.IMPORTANCE_MAX);
        }
    }
//建立通知渠道
    @TargetApi(Build.VERSION_CODES.O)
    private void createChinnel(String channelId, String channelName, int impor) {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(channelId,channelName,impor);
        manager.createNotificationChannel(channel);
    }

    private void init() {
        mNoti1 = findViewById(R.id.noti1);
        mNoti2 = findViewById(R.id.noti2);
        mNoti3 = findViewById(R.id.noti3);

        mToolbar = this.findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        mToolbar.setTitle("通知学习");

        mNoti1.setOnClickListener(this);
        mNoti2.setOnClickListener(this);
        mNoti3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.noti1:
                buildNotificationOne();
                break;
            case R.id.noti2:
                buildNotificationTwo();
                break;
            case R.id.noti3:
                buildNotificationThree();
                break;

        }
    }
    //builder.setVisibility()，用于设置显示等级
    //VISIBILITY_PUBLIC:任何情况都会显示通知
    //VISIBILITY_PRIVATE:只有在没有所平时才会显示通知
    //VISIBILITY_SECRET:在pin,password等安全所和没有所平的情况下才能够显示通知
//悬挂模式的通知,消息球
    private void buildNotificationThree() {
        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder
                (this,"suspension");//一定要加管道的ID，不然就会为null而不显示

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
        //上面根据URI的转换，点击之后会跳转到该网站的intent设置
        builder.setContentIntent(pi);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentTitle("悬挂通知");
        builder.setContentText("受到一条悬挂的通知，点击回主页");
        builder.setWhen(System.currentTimeMillis());
        builder.setNumber(3);

        //设置点击跳转,悬挂式
        Intent t = new Intent();
        t.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        t.setClass(this,MainActivity.class);
        PendingIntent pt = PendingIntent.getActivity
                (this,0,t,PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(pt,true);

        mNotificationManager.notify(3,builder.build());
    }

    //显示一条折叠模式的通知
    private void buildNotificationTwo() {
        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder
                (this,"fold");//一定要加管道的ID，不然就会为null而不显示

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
        //上面根据URI的转换，点击之后会跳转到该网站的intent设置
        builder.setContentIntent(pi);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentTitle("折叠通知");
        builder.setContentText("受到一条折叠的通知");
        builder.setWhen(System.currentTimeMillis());
        builder.setNumber(2);

        //用 RemoteViews来创建自定义Notification视图
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.foldnoti_layout);
        Notification notification = builder.build();
        notification.bigContentView = remoteViews;

        mNotificationManager.notify(2,notification);

    }
    //显示一条普通模式的通知。

    private void buildNotificationOne() {

        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder
                (this,"generate");

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
        //上面根据URI的转换，点击之后会跳转到该网站的intent设置
        builder.setContentIntent(pi);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentTitle("普通通知");
        builder.setContentText("受到一条普通的通知");
        builder.setWhen(System.currentTimeMillis());
        builder.setNumber(1);

        mNotificationManager.notify(1,builder.build());

    }
}
